<template>
	<el-container>
		<!-- 页面头部 -->
		<el-header>
			<span>电商后台管理系统</span>
			<el-button type="danger" @click='logout'>退出登录</el-button>
		</el-header>
		<!-- 页面主体部分 -->
		<el-container>
			<!-- 侧边栏 -->
			<el-aside :width="isCollapse?'64px':'200px'">
				<div id = 'menufold' @click="foldMenus()"><i class="el-icon-s-fold" ></i></div>
				<!-- 侧边菜单 -->
				<el-menu  background-color="#303133" text-color="#fff" active-text-color="#409efe" 
				:collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
					<el-submenu :index="menu.id+''" v-for="menu in menulist" :key='menu.id'>
						<template slot="title">
							<i :class="iconlist[menu.id]"></i>
							<span>{{menu.authName}}</span>
						</template>
						
						<el-menu-item :index="'/'+submenu.path" v-for='submenu in menu.children' 
						:key='submenu.id' @click="saveNavState('/'+submenu.path)">
							<i class="el-icon-menu"></i>
							<span>{{submenu.authName}}</span>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<!-- 内容主体 -->
			<el-main>
				<router-view>hhh</router-view>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
	export default {
		data(){
			return{
				menulist:[],
				iconlist:{
					125:'el-icon-user-solid',
					103:'el-icon-s-tools',
					101:'el-icon-s-goods',
					102:'el-icon-s-order',
					145:'el-icon-s-marketing'
				},
				isCollapse:false,
				activePath:''
			}
		},
		created() {
			this.getMenus();
			this.activePath = window.sessionStorage.getItem('activePath');
		},
		methods: {
			logout() {
				window.sessionStorage.clear();
				this.$router.push('/login');
			},
			async getMenus(){
				const {data:res} = await this.$http.get('/menus');
				if(res.meta.status!=200) return this.$message.error(res.meta.msg);
				this.menulist = res.data;
				console.log(res);
				
			},
			foldMenus(){
				this.isCollapse = !this.isCollapse;
			},
			saveNavState(path){
				console.log(path);
				window.sessionStorage.setItem('activePath',path);
				this.activePath = path;
				
			}
		}
	}
</script>
<style scoped>
	.el-header {
		background-color: #373d3f;
		color: #fff;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 20px;
	}

	.el-aside {
		background-color: #303133;
	}
	.el-aside .el-menu{
		border-right:none;
	}
	.el-main {
		background-color: #eaedf1;
	}

	.el-container {
		height: 100%;
	}
	#menufold{
		color: #FFFFFF;
		text-align: center;
		cursor: pointer;
		line-height: 24px;
	}
</style>
